package com.learn.springlearn.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.learn.springlearn.dao.CarDocumentMapper;
import com.learn.springlearn.entity.CarDocument;
import com.rabbitmq.tools.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.Random;
import java.util.concurrent.Executor;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("es")
public class EsController {


    CarDocumentMapper carDocumentMapper;


    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    public EsController(CarDocumentMapper carDocumentMapper, ElasticsearchRestTemplate elasticsearchRestTemplate) {
        this.carDocumentMapper = carDocumentMapper;
        this.elasticsearchRestTemplate = elasticsearchRestTemplate;
    }

    @GetMapping("save")
    public String put() {
        CarDocument carDocument = new CarDocument();
        carDocument.setCarId(4);
        carDocument.setName("smp ");
        carDocument.setBeginTime(new Date());
        CarDocument save = carDocumentMapper.save(carDocument);
        return "success";
    }

    @GetMapping("deleted")
    public String deleted() {
         carDocumentMapper.deleteAll();
        return "success";
    }

    @GetMapping("get")
    public CarDocument get() {

        Optional<CarDocument> byId = carDocumentMapper.findById(3);
        CarDocument carDocument = byId.get();
        return carDocument;
    }

    @GetMapping("find")
    public String find() {

         BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
//        boolQueryBuilder.must(QueryBuilders.matchPhrasePrefixQuery("name", "测"));
//        boolQueryBuilder.must(QueryBuilders.matchQuery("name","sdj"));
//        boolQueryBuilder.must(QueryBuilders.matchPhraseQuery("name","sdj"));
//        boolQueryBuilder.must(new MatchPhraseQueryBuilder("name","te"));
//        boolQueryBuilder.must(new TermQueryBuilder("name","sdj"));
//        boolQueryBuilder.must(new FuzzyQueryBuilder("name","sdj"));
//        LocalDateTime start = LocalDateTime.of(2020, 12, 1, 0, 0);
//        LocalDateTime end = LocalDateTime.of(2020, 12, 18, 23, 0);
//        long startMill = start.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
//        long endMill = end.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
//        boolQueryBuilder.must(new RangeQueryBuilder("beginTime").gt(startMill).lt(endMill));
        boolQueryBuilder.must(new TermsQueryBuilder("_id", Lists.newArrayList(1,2)));
//        boolQueryBuilder.must(QueryBuilders.rangeQuery("beginTime").lte(endMill));
        PageRequest page = PageRequest.of(0, 10);
        FieldSortBuilder sort = SortBuilders.fieldSort("beginTime").order(SortOrder.DESC);
        NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withQuery(boolQueryBuilder)
                .withSort(sort)
                .withPageable(page).build();
        SearchHits<CarDocument> search = elasticsearchRestTemplate.search(searchQuery, CarDocument.class);
        System.out.println(search.getTotalHits());
        List<CarDocument> collect = search.getSearchHits().stream().map(SearchHit::getContent).collect(Collectors.toList());
        collect.forEach(carDocument -> System.out.println(JSON.toJSONString(carDocument)));

        return "success";
    }


}
